package com.byq.mm.smartlib.activity;


import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

import com.byq.mm.abstractx.cluster.client.DeviceClient;
import com.byq.mm.abstractx.device.Cluster;
import com.byq.mm.smartlib.R;
import com.byq.mm.smartlib.application.SmartApplication;
import com.byq.mm.smartlib.cluster.activity.client.ClientClusterActivityManager;

public class DeviceActivity extends TabActivity {
	 protected SmartApplication application;
	Button back;
	  @Override
	  public void onCreate(Bundle icicle) {
	    super.onCreate(icicle);
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams. FLAG_FULLSCREEN);
	    application = (SmartApplication) this.getApplication();
	    String globalId = this.getIntent().getExtras().getString("globalId");
	    DeviceClient device = application.getAndroidSmartEngine().getCoreEngine().getClientDeviceManager().getDeviceClient(globalId);
		this.setContentView(R.layout.device_activity_host);
		TabHost tabHost = findViewById(android.R.id.tabhost);
		back = (Button)this.findViewById(R.id.back_device_activity);
		  back.setOnClickListener(new View.OnClickListener(){

			  @Override
			  public void onClick(View view) {
				  DeviceActivity.this.finish();
			  }
		  });

		  tabHost.setup();

	 LayoutInflater mInflater = LayoutInflater.from(this);
	 for(Cluster c : device.getDevice().getSupportedClusters()){
		 String cid = c.getId();

		 Class<? extends Activity> type = ClientClusterActivityManager.getActivity(cid);
		 Intent it = new Intent(this,type);
		 it.setPackage(this.getPackageName());
		 it.putExtra("globalId",device.getDevice().getGlobalId());
		 it.putExtra("clusterId",c.getId());
		 String title = c.getAlias();
		 if(title == null || title.equals("")){
			 int rid = getResources().getIdentifier(c.getId(),  "string",getPackageName());
			 title = getResources().getString(rid);
		 }
		 int irid = getResources().getIdentifier(c.getId(),  "drawable",getPackageName());

		 
		 View v = mInflater.inflate(R.layout.cluster_icon_text, null);
		 TextView text = (TextView)v.findViewById(R.id.text);
		 ImageView iv = (ImageView)v.findViewById(R.id.icon);
		 text.setText(title);
		 iv.setImageResource(irid);
TabSpec tab = tabHost.newTabSpec(c.getId())
	                  .setIndicator(v)
	                  .setContent(it);
	                  tabHost.addTab(tab);
	 }
	 
    }

}
